package com.zxm.common.utils.sms;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: silence.Z
 * @CreateTime: 2021/5/7 16:14
 * @Description:
 */
public class SMSUtil {
	private static final Logger log = LoggerFactory.getLogger(SMSUtil.class);
	private static final Integer appid = 1400516550;
	private static final String appkey = "4c2b6a5be650c7a82bb6875f4388ac47";
	private static final String smsSign = "安之盾社区";

	public static Map<String, Object> sendSMS(String phone, Integer templateId, String code) {
		String res = null;
		Map<String, Object> map = new HashMap<>();
		//随机生成六位验证码的
		log.info("腾讯云发送短消息。phone:" + phone + ";code:" + code + ";模版ID:" + templateId);
		try {
			//参数，一定要对应短信模板中的参数顺序和个数，
			String[] params = new String[2];
			params[0] = code;
			params[1] = "5";
			//创建sender对象
			SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
			//发送
			SmsSingleSenderResult result = ssender.sendWithParam("86", phone, templateId, params, smsSign, "", "");
			log.info("短信发送结果："+result);
			if (result.result != 0) {
				res = Boolean.FALSE.toString();
				map.put("data", res);
			} else {
				res = Boolean.TRUE.toString();
				map.put("data", res);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	public static Map<String, Object> sendSMSMessage(String phone, String message) {
		String res = null;
		Map<String, Object> map = new HashMap<>();
		log.info("腾讯云发送短消息。phone:" + phone + ";message:" + message + ";模版ID:" + 950031);
		try {
			String[] params = new String[2];
			params[0] = message;
			params[1] = "5";
			SmsSingleSenderResult result = new SmsSingleSender(appid, appkey).sendWithParam("86", phone, 950031, params, smsSign, "", "");
			log.info("短信发送结果："+result);
			if (result.result != 0) {
				res = Boolean.FALSE.toString();
			} else {
				res = Boolean.TRUE.toString();
			}
			map.put("data", res);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	public static void main(String[] args) {
		String code = Integer.valueOf((int) ((Math.random() * 9.0D + 1.0D) * 100000.0D)).toString();
		sendSMS("18770968150", 950031, code);
	}
}
